<%@ include file="Common.jsp" %>
<%@ page import="de.localsport.util.Mailer" %>
<%@ page import="de.localsport.util.PasswordManager" %>

<%!
//
//   Filename: Registration.jsp
//   Generated with CodeCharge  v.1.2.0
//   JSP.ccp build 05/21/01
//

static final String sFileName = "Registration_Mod.jsp";
              
%>
<%

boolean bDebug = false;

String sAction = getParam( request, "FormAction");
String sForm = getParam( request, "FormName");
String sFormErr = "";

java.sql.Connection conn = null;
java.sql.Statement stat = null;
String sErr = loadDriver();
conn = cn();
stat = conn.createStatement();
if ( ! sErr.equals("") ) {
 try {
   out.println(sErr);
 }
 catch (Exception e) {}
}
if ( sForm.equals("Form") ) {
  sFormErr = FormAction(request, response, session, out, sAction, sForm, conn, stat);
  if ( "sendRedirect".equals(sFormErr)) return;
}

%>            
<html>
<head>
<title>Zoccer fuer Profis - Regsitrierung</title>
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="expires" content="0"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body font-family: Arial, Tahoma, Verdana, Helveticabackground-color: #FFFFFF; color: #000000; font-family: Arial, Tahoma, Verdana, Helvetica">
<jsp:include page="Header.jsp" flush="true"/><center>
 <table>
  <tr>
   
   <td valign="top">
<% Form_Show(request, response, session, out, sFormErr, sForm, sAction, conn, stat); %>
    
   </td>
  </tr>
 </table>

<jsp:include page="Footer.jsp" flush="true"/>

</body>
</html>
<%%>
<%
if ( stat != null ) stat.close();
if ( conn != null ) conn.close();
%>
<%!


  String FormAction(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sAction, String sForm, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
  
    String sFormErr ="";
    java.sql.ResultSet rs = null;
    
    try {

      if (sAction.equals("")) return "";

      String sSQL="";      
      String transitParams = "";
      String primaryKeyParams = "";
      String sQueryString = "";
      String sPage = "";
      String sParams = "";
      String sActionFileName = "Registration_Success.jsp";
      String sWhere = " ";
      boolean bErr = false;
      long iCount = 0;
      String sUserRights = "";
  
      String pPKmember_id = "";

      final int iinsertAction = 1;
      final int iupdateAction = 2;
      final int ideleteAction = 3;
      int iAction = 0;

      if ( sAction.equalsIgnoreCase("insert") ) { iAction = iinsertAction; }
      if ( sAction.equalsIgnoreCase("update") ) { iAction = iupdateAction; }
      if ( sAction.equalsIgnoreCase("delete") ) { iAction = ideleteAction; }      

      // Load all form fields into variables    
      String fldmember_login = getParam(request, "member_login");
      String fldmember_password = getParam(request, "member_password");
      String fldmember_password_repeat = getParam(request, "member_password_repeat");
      String fldfirst_name = getParam(request, "first_name");
      String fldlast_name = getParam(request, "last_name");
      String fldemail = getParam(request, "email");      
      String fldnickname= getParam(request, "nickname");
      String fldmember_id= getParam(request, "member_id");

	  // Non form fields
	  String mailTo = null;
	  String initialPw = null;
	  boolean sendPwMail = false;
      
      // Validate fields
      if ( iAction == iinsertAction || iAction == iupdateAction ) {
        if ( isEmpty(fldmember_login) ) {
          sFormErr = sFormErr + "The value in field EIN is required.<br>";
        }        
        if ( isEmpty(fldnickname) ) {
          sFormErr = sFormErr + "The value in field Nickname is required.<br>";
        }

        if (sFormErr.length() > 0 ) {
          return (sFormErr);
        }
        
		// -----------------------------------------------------------------------
        // Check, if EIN is valid and not already registered
		// -----------------------------------------------------------------------
		//
	  	sSQL = "";
	  	sSQL = "select * from members where member_login=" + toSQL(fldmember_login, adText);
	  	
	  	rs = openrs( stat, sSQL);
	  	if (! rs.next()) {
	  		sFormErr = sFormErr + "Invalid EIN, Sorry.<br>";
	  	} else {
	  		int iIsRegistered = rs.getInt("registered");
	  		if (iIsRegistered == 1){
	  			sFormErr = sFormErr + "EIN is already registered. Forgot password?<br>";
	  		} else {
	  			sUserRights = rs.getString("security_level_id");	  		
	  		}
		}
		if (sFormErr.length() > 0 ) {
          return (sFormErr);
        }  

		mailTo=rs.getString("email");
		if (isEmpty(mailTo) ) {
			sFormErr = sFormErr + "No email address assigned to EIN. Please contact Administrator.<br>";
		}
        
        if (sFormErr.length() > 0 ) {
          return (sFormErr);
        }    

		// -----------------------------------------------------------------------
        // Check if Nickname is already used
		// -----------------------------------------------------------------------
		//

	  	sSQL = "";
	  	sSQL = "select * from members where UPPER(nickname)=" + toSQL(fldnickname.toUpperCase(), adText);
	  	
	  	rs = openrs( stat, sSQL);
	  	if (rs.next()) {
	  		sFormErr = sFormErr + "Nickname is already in use. Please try another one.<br>";
	  	} 
        
        if (sFormErr.length() > 0 ) {
          return (sFormErr);
        }	

		// -----------------------------------------------------------------------
        // Create password
		// -----------------------------------------------------------------------
		//
		initialPw = PasswordManager.createPassword();
      }

		// -----------------------------------------------------------------------
        //  Execute Action
		// -----------------------------------------------------------------------
		//

      sSQL = "";
      // Create SQL statement

      switch (iAction) {
  
        case iupdateAction :                   		
          		final String sIsRegistered = "1";
          		sSQL = "update members set " +
                "member_password=" + toSQL(initialPw, adText) + ", " +
                "nickname=" + toSQL(fldnickname, adText) + ", " +
                "registered=" + toSQL(sIsRegistered, adNumber) + " " +
                "where member_login=" + toSQL(fldmember_login, adText);
				sendPwMail  = true;       		
          break;
      }

      if ( sFormErr.length() > 0 ) return sFormErr;
      try {
		if (sendPwMail) {
			Mailer m = new Mailer();
			int mailStatus = m.sendPassword(mailTo, initialPw);
			if (mailStatus != 0) {
				sFormErr += "Sending of registration email failed. Please contact the administrator.<br>";
			} else {
				// Execute SQL statement
				stat.executeUpdate(sSQL);
			}
		}
      }
      catch(java.sql.SQLException e) {
		if (conn != null) conn.rollback();
        sFormErr = e.toString(); return (sFormErr);
      }
  
      try {
        if ( stat != null ) stat.close();
        if ( conn != null ) conn.close();
      }
      catch ( java.sql.SQLException ignore ) {}
      
      //Do login
      session.setAttribute("UserID", fldmember_id);            
      session.setAttribute("UserRights", sUserRights);
	  session.setAttribute("EmailAddress", mailTo);
      
      //Forward to Spielerpass
      response.sendRedirect (sActionFileName);

      return "sendRedirect";
    }
    catch (Exception e) {out.println(e.toString()); }
    return (sFormErr);
  }


  void Form_Show(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sFormErr, String sForm, String sAction, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
    try {

      String sSQL="";
      String sQueryString = "";
      String sPage = "";
      String sWhere = "";
      String transitParams = "";
      String transitParamsHidden = "";
      String requiredParams = "";
      String primaryKeyParams ="";
      java.util.Hashtable rsHash = new java.util.Hashtable();
      
      String pmember_id = "";

      String fldmember_id="";
      String fldmember_login="";
      String fldmember_password="";
      String fldmember_password_repeat="";
      String fldfirst_name="";
      String fldlast_name="";
      String fldemail="";
      String fldnickname="";

      boolean bPK = true;

      if ( "".equals(sFormErr)) {
        // Load primary key and form parameters
        pmember_id = getParam( request, "member_id");
      }
      else {
        // Load primary key, form parameters and form fields
        fldmember_id = getParam( request, "member_id");
        fldmember_login = getParam( request, "member_login");
        fldmember_password = getParam( request, "member_password");
        fldmember_password_repeat = getParam( request, "member_password_repeat");
        fldfirst_name = getParam( request, "first_name");
        fldlast_name = getParam( request, "last_name");
        fldemail = getParam( request, "email");
        fldnickname = getParam( request, "nickname");
        pmember_id = getParam( request, "PK_member_id");
      }
      
      if ( isEmpty(pmember_id)) { bPK = false; }
      
      out.println(getParam( request, "PK_member_id"));
      
      sWhere += "member_id=" + toSQL(pmember_id, adNumber);
      primaryKeyParams += "<input type=\"hidden\" name=\"PK_member_id\" value=\""+pmember_id+"\"/>";

      sSQL = "select * from members where " + sWhere;

	  out.println("&nbsp;<br><br><br><br>");
      out.println("<table bgcolor=\"#000000\" cellspacing=\"1\" cellpadding=\"1\">");
      out.println("<tr>\n<td style=\"background:rgb(235,235,235); text-align: Center\" colspan=\"2\"><font style=\"font-size: 12pt; font-weight: bold\">Mitspieler Registrierung</font></td>\n</tr>");
      if ( ! sFormErr.equals("")) {
        out.println("     <tr>\n      <td style=\"background-color: #000000; border-style: inset; border-width: 1\" colspan=\"2\"><font style=\"font-size: 10pt; color: #000000\">"+sFormErr+"</font></td>\n     </tr>");
      }
      sFormErr="";
      out.println("     <form method=\"get\" action=\""+sFileName+"\" name=\"Form\">");

      java.sql.ResultSet rs = null;

      if ( bPK &&  ! (sAction.equals("insert") && "Form".equals(sForm))) {

        // Open recordset
        rs = openrs( stat, sSQL);
        rs.next();
        String[] aFields = getFieldsName( rs );
        getRecordToHash( rs, rsHash, aFields );
        rs.close();
        fldmember_id = (String) rsHash.get("member_id");
        if ( "".equals(sFormErr)) {
          // Load data from recordset when form displayed first time
          fldmember_login = (String) rsHash.get("member_login");
          fldmember_password = (String) rsHash.get("member_password");
          fldmember_password_repeat = (String) rsHash.get("member_password");
          fldfirst_name = (String) rsHash.get("first_name");
          fldlast_name = (String) rsHash.get("last_name");
          fldemail = (String) rsHash.get("email");
          fldnickname = (String) rsHash.get("nickname");       
        }

        if (sAction.equals("") || ! "Form".equals(sForm)) {
      
          fldmember_id = (String) rsHash.get("member_id");
          fldmember_login = (String) rsHash.get("member_login");
          fldmember_password = (String) rsHash.get("member_password");
          fldmember_password_repeat = (String) rsHash.get("member_password");
          fldfirst_name = (String) rsHash.get("first_name");
          fldlast_name = (String) rsHash.get("last_name");
          fldemail = (String) rsHash.get("email");
          fldnickname = (String) rsHash.get("nickname");
        }        
        
      }
      else {
      }

      // Show form field
      
      out.print("     <tr>\n      <td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 10pt; color: #000000\">EIN</font></td><td>"); out.print("<input type=\"text\"  name=\"member_login\" maxlength=\"50\" value=\""+toHTML(fldmember_login)+"\">");
      out.println("</td>\n     </tr>");
      out.print("     <tr>\n      <td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 10pt; color: #000000\">Nickname</font></td><td>"); out.print("<input type=\"text\"  name=\"nickname\" maxlength=\"50\" value=\""+toHTML(fldnickname)+"\">");
      out.println("</td>\n     </tr>");
      /*
      out.print("     <tr>\n      <td style=\"background:rgb(235,235,235); \"><font style=\"font-size: 10pt; color: #ffffff\">Name</font></td><td style=\"background:rgb(235,235,235); \">"); out.print("<input type=\"text\"  name=\"first_name\" maxlength=\"50\" value=\""+toHTML(fldfirst_name)+"\" size=\"15\" readonly>");
      out.println("</td>\n     </tr>");
      out.print("     <tr>\n      <td style=\"background:rgb(235,235,235); \"><font style=\"font-size: 10pt; color: #ffffff\">Given Name</font></td><td style=\"background:rgb(235,235,235); \">"); out.print("<input type=\"text\"  name=\"last_name\" maxlength=\"50\" value=\""+toHTML(fldlast_name)+"\" size=\"15\" readonly>");
      out.println("</td>\n     </tr>");
      out.print("     <tr>\n      <td style=\"background:rgb(235,235,235); \"><font style=\"font-size: 10pt; color: #ffffff\">Email Address</font></td><td style=\"background:rgb(235,235,235); \">"); out.print("<input type=\"text\"  name=\"email\" maxlength=\"50\" value=\""+toHTML(fldemail)+"\" size=\"15\" readonly>");
      out.println("</td>\n     </tr>");
      */
      out.print("     <tr>\n      <td style=\"background:rgb(235,235,235);\" colspan=\"2\" align=\"left\">");


      if ( bPK && ! (sAction.equals("insert") && "Form".equals(sForm))) {
        
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"Form\"><input type=\"hidden\" value=\"\" name=\"FormAction\">");
      }
      
      else {
        out.print("<input type=\"submit\" value=\"Register\" onclick=\"document.Form.FormAction.value = 'update';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"Form\"><input type=\"hidden\" value=\"update\" name=\"FormAction\">");
      }out.print("<input type=\"hidden\" name=\"member_id\" value=\""+toHTML(fldmember_id)+"\">");
      out.print(transitParamsHidden+requiredParams+primaryKeyParams);
      out.println("</td>\n     </tr>\n     </form>\n    </table><br><br>");

    }
    catch (Exception e) { out.println(e.toString()); }
  } %>